package org.jeecg.modules.nikeProject.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsReference;
import org.jeecg.modules.nikeProject.entity.vo.SportsReferenceVO;
import org.jeecg.modules.nikeProject.service.ISportsProjectService;
import org.jeecg.modules.nikeProject.service.ISportsReferenceService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 申报推荐人
 * @Author: jeecg-boot
 * @Date:   2024-12-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags="206-申报推荐人")
@RestController
@RequestMapping("/sportsReference")
public class SportsReferenceController extends JeecgController<SportsReference, ISportsReferenceService> {
	@Autowired
	private ISportsReferenceService sportsReferenceService;

	@Autowired
	private ISportsProjectService sportsProjectService;
	
	/**
	 * 分页列表查询
	 *
	 * @param sportsReference
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
//	@AutoLog(value = "申报推荐人-分页列表查询")
//	@ApiOperation(value="申报推荐人-分页列表查询", notes="申报推荐人-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SportsReference sportsReference,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SportsReference> queryWrapper = QueryGenerator.initQueryWrapper(sportsReference, req.getParameterMap());
		Page<SportsReference> page = new Page<SportsReference>(pageNo, pageSize);
		IPage<SportsReference> pageList = sportsReferenceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param sportsReferenceVO
	 * @return
	 */
	@AutoLog(value = "申报推荐人-添加")
	@ApiOperation(value="申报推荐人-添加", notes="申报推荐人-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SportsReferenceVO sportsReferenceVO, HttpServletRequest request) {
		String username = JwtUtil.getUserNameByToken(request);
		return sportsReferenceService.add(username, sportsReferenceVO);
	}
	
	/**
	 * 编辑
	 *
	 * @param sportsReferenceVO
	 * @return
	 */
	@AutoLog(value = "申报推荐人-编辑")
	@ApiOperation(value="申报推荐人-编辑", notes="申报推荐人-编辑")
	@PostMapping(value = "/edit")
//	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<?> edit(@RequestBody SportsReferenceVO sportsReferenceVO, HttpServletRequest request) {
		String username = JwtUtil.getUserNameByToken(request);
//		String username = "18019101986";
		String projectId = sportsReferenceVO.getProjectId();
		SportsProject sportsProject = sportsProjectService.getSportsProjectById(projectId);
		if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
			return Result.error("案例已提交不能编辑！");
		}
		List<SportsReference> sportsReferences = sportsReferenceVO.getSportsReferences();
		sportsReferences.forEach(x ->{
			SportsReference sportsReference = sportsReferenceService.getById(x.getId());
			if (sportsReference != null) {
				x.setUpdateBy(username);
				x.setUpdateTime(new Date());
				sportsReferenceService.updateById(x);
			} else {
				x.setCreateTime(new Date());
				x.setCreateBy(username);
				x.setProjectId(projectId);
				sportsReferenceService.save(x);
			}
		});
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
//	@AutoLog(value = "申报推荐人-通过id删除")
//	@ApiOperation(value="申报推荐人-通过id删除", notes="申报推荐人-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sportsReferenceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
//	@AutoLog(value = "申报推荐人-批量删除")
//	@ApiOperation(value="申报推荐人-批量删除", notes="申报推荐人-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sportsReferenceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
//	@AutoLog(value = "申报推荐人-通过id查询")
//	@ApiOperation(value="申报推荐人-通过id查询", notes="申报推荐人-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SportsReference sportsReference = sportsReferenceService.getById(id);
		return Result.OK(sportsReference);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param sportsReference
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, SportsReference sportsReference) {
      return super.exportXls(request, sportsReference, SportsReference.class, "申报推荐人");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, SportsReference.class);
  }

}
